package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.liangmutian.mypicker.DateUtil;
import com.hdjk.view.R;
import com.hdjk.view.entity.HistoricalTransactionEntity;
import com.hdjk.view.util.StringAppUtil;

import java.util.List;

import static com.hdjk.view.R.id.tv_date;

/**
 * Created by sxy on 2017/11/3.
 */

public class HistoricalTransactionAdapter extends BaseAdapter {
    private List<HistoricalTransactionEntity> mList;
    private Context mContext;

    public HistoricalTransactionAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<HistoricalTransactionEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public HistoricalTransactionEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_historical_transaction, null);
            holder.tv_buy = (TextView) convertView.findViewById(R.id.tv_buy);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.tv_date = (TextView) convertView.findViewById(tv_date);
            holder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        HistoricalTransactionEntity entity = mList.get(position);
        holder.tv_buy.setText(entity.getTransactionTypeName());
        if (StringAppUtil.isEmptyOrNull(entity.getTransactionDate())) {
            holder.tv_date.setText("订单尚未下单成功");
        } else {
            String date = DateUtil.formatDate(Long.parseLong(entity.getTransactionDate()), "yyyy-MM-dd");
            holder.tv_date.setText(date);
        }
        if (StringAppUtil.isEmptyOrNull(entity.getTransactionAmount())){
            holder.tv_money.setText("订单尚未确认");
        }else {
            holder.tv_money.setText(entity.getTransactionAmount() + "元");
        }
        holder.tv_name.setText(entity.getFundName());
        if (StringAppUtil.isEmptyOrNull(entity.getTransactionStatus())){
            holder.tv_state.setVisibility(View.GONE);
        }else {
            holder.tv_state.setVisibility(View.VISIBLE);
            holder.tv_state.setText(entity.getTransactionStatusName());
        }
        return convertView;
    }

    class Holder {
         TextView tv_buy;
         TextView tv_name;
         TextView tv_money;
         TextView tv_date;
         TextView tv_state;
    }
}
